# -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 23:04:04 2012

@author: Maxim
"""
import airfoil
import matplotlib.pyplot as plt
import numpy as ny
from matplotlib import rc
from scipy.interpolate import interp1d

#def write_afl(path,af,polar):
#    pass


def process(path,M,Re,display):
    alphaSeq1 = [-180,-21,2.5]
    alphaSeq2 = [-20,19,1.0]
    alphaSeq3 = [20,180,2.5]
    alphaSeq4 = ny.hstack([ny.arange(-180,-20,1.0),ny.arange(-20,20,0.1),ny.arange(20,181,1.0)])

    af = airfoil.Airfoil()
    af.read_txt(path)
    polar1 = af.get_J_polar(M,Re,alphaSeq1)
    polar2 = af.get_J_polar(M,Re,alphaSeq2)
    polar3 = af.get_J_polar(M,Re,alphaSeq3)
    alpha = ny.hstack([polar1.alpha,polar2.alpha,polar3.alpha])
    cl = ny.hstack([polar1.cl,polar2.cl,polar3.cl])
    cd = ny.hstack([polar1.cd,polar2.cd,polar3.cd])
    cm = ny.hstack([polar1.cm,polar2.cm,polar3.cm])
    clInt = interp1d(alpha,cl,'cubic')
    cdInt = interp1d(alpha,cd,'linear')
    cmInt = interp1d(alpha,cm,'cubic')
    cl = clInt(alphaSeq4)
    cd = cdInt(alphaSeq4)
    cm = cmInt(alphaSeq4)
    alpha = alphaSeq4
    
    afRedimPts = af.redim(14)
    af.analyze_geometry()
    tc = af.thickness
    afRedimPts[:,1] = afRedimPts[:,1] / tc
    
    if display:
        plt.figure(1)
        plt.plot(af.coord[:,0],af.coord[:,1],'r-')
        plt.hold(True)
        plt.grid(True)
        plt.axis('equal')
        plt.plot(afRedimPts[:,0],afRedimPts[:,1],'o-')
        plt.legend(['original','processed'])
        
        plt.figure(2)
        plt.plot(alpha, cl, 'b-')
        plt.xlabel('alpha, deg')
        plt.ylabel('lift coefficient')
        plt.axis([-180,180,-2,2])
        plt.hold(True)
        plt.plot([-20.,-20],[-2,2.],'r')
        plt.plot([20.,20],[-2,2.],'r')
        plt.grid(True)
        plt.show()
    return alpha, cl, cd, cm, afRedimPts, tc

def write_afl(path,alpha,cl,cd,cm,afPts,Re,tc):
    afl = open(path,'wt')
    afl.write('I\n')
    afl.write('900 version\n')
    afl.write('1234 device type code\n')
    afl.write('%.6f  %.6f'%(Re/1e6,tc))
    dataList = [0.75,0.105,0.15,-18,18,10,1.4,1.8,0,0.06,1.0,0.006,0,0,0,0.1,0.013,2.3,0,0,0,0,-15,15,0.117,-0.012,-0.012,-0.117]
    for data in dataList: afl.write('%.6f '%data)
    afl.write('\n')
    afl.write('alpha cl cd cm:\n')
    for i,a in enumerate(alpha):
        afl.write('%.1f  %.5f  %.5f  %.5f\n'%(a,cl[i],cd[i],cm[i]))
    for point in afPts:
        afl.write('%.5f  %.5f\n'%(point[0],point[1]))
    afl.close()

def create_afl_from_txt(airfoilPath,aflPath,Mach=0.15,Re=3.0e6,display=False):
    alpha, cl, cd, cm, afpts, thickness = process(airfoilPath,Mach,Re,display)
    write_afl(aflPath,alpha,cl,cd,cm,afpts,Re,thickness)

def run_test1():
    afPath = 'GA37A315mod_reworked.dat' # input airfoil text file
    aflPath = 'GA37A315mod.afl' #output afl xplane file
    Mach = 0.15
    Re = 3.0e6
    display = True
    create_afl_from_txt(afPath,aflPath,Mach,Re,display)

if __name__=="__main__":
    run_test1()
